package com.wangsd.web.controller.account;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.entity.account.Payout;
import com.wangsd.web.service.account.PayoutService;
import com.wangsd.web.utils.ErrorTips;
import com.wangsd.web.utils.PageUtils;
import com.wangsd.web.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/account/bill")
@Api(tags = "账单记录操作接口")
public class BillController {

    @Resource
    private PayoutService payoutService;

    /**
     * 记账
     * @param payout 出账信息对象
     */
    @PostMapping("/keep")
    @ApiOperation("记账")
    public R keep(@ApiParam(name = "payout", value = "账单对象", type = "Payout") Payout payout) {
        if (payout != null && payoutService.save(payout)) {
            return R.success();
        }
        return R.error(ErrorTips.UN_SAVE);
    }

    /**
     * 修改账目信息
     */
    @PutMapping("/modify")
    @ApiOperation("修改账单信息")
    public R modify(@ApiParam(name = "payout", value = "账单对象", type = "Payout") Payout payout) {
        if (payout != null) {
            if (payout.getBillId() != null) {
                return payoutService.modify(payout) ? R.success() : R.error(ErrorTips.UN_SAVE);
            } else {
                R.error(ErrorTips.UN_KNOW_COLUMN);
            }
        }
        return R.error(ErrorTips.NO_SUCH_DATA);
    }

    /**
     * 删除账目
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除账单")
    public R delete(@ApiParam(name = "billId", value = "账单Id", type = "Integer") Integer billId) {
        if (billId != null) {
            return payoutService.delete(billId) ? R.success() : R.error(ErrorTips.UN_KNOW_COLUMN);
        }
        return R.error(ErrorTips.UN_DELETE);
    }

    /**
     * 根据主键ID查询记录
     */
    @GetMapping("/no")
    @ApiOperation("通过主键Id查询账单信息")
    public R getById(@ApiParam(name = "billId", value = "账单Id", type = "Integer") Integer billId) {
        if (billId != null) {
            Payout payout = payoutService.getDetailsById(billId);
            return payout != null ? R.success().put("detail", payout) : R.error(ErrorTips.NO_SUCH_DATA);
        }
        return R.error(ErrorTips.UN_KNOW_COLUMN);
    }

    /**
     * 通过用户ID查询用户消费记录
     */
    @GetMapping("/all/no")
    @ApiOperation("通过用户Id查询用户的所有账单信息")
    public R getAllByUserNo(@ApiParam(name = "userId", value = "用户Id", type = "Integer") Integer userId) {
        if (userId != null) {
            List<Payout> payoutList = payoutService.getAllByUserId(userId);
            return payoutList != null ? R.success().data(payoutList) : R.error(ErrorTips.NO_SUCH_DATA);
        }
        return R.error(ErrorTips.UN_KNOW_COLUMN);
    }

    /**
     * 根据页码进行分页查询
     */
    @GetMapping("/page")
    @ApiOperation("通过用户Id对用户账单信息进行分页查询")
    public R getByPageNo(@ApiParam(name = "pageNum", value = "页码", type = "Integer") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNun,
                         @ApiParam(name = "userId", value = "用户Id", type = "Integer") Integer userId) {
        if (userId == null) return R.error(ErrorTips.UN_KNOW_COLUMN);
        List<Payout> payoutList = payoutService.getTenByUserId(userId, new Page<Payout>(pageNun, PageUtils.getPageSize()));
        return payoutList != null ? R.success().data(payoutList) : R.error(ErrorTips.NO_SUCH_DATA);
    }
}